// 滑动窗口

var lengthOfLongestSubstring = function(s) {

  const len = s.length;
  if (len <= 1) return len;

  let window = new Set();
  let ans = 0, left = 0, right = 0;

  while(right < len) {
    if (!window.has(s[right])) {
      window.add(s[right]);
      right++;
    } else {
      while(window.has(s[right])) {
        window.delete(s[left]);
        left++;
      }
    }
    ans = Math.max(ans, window.size)
  }

  return ans;
};


const s = "abcabcbb";
console.log(lengthOfLongestSubstring(s))
